﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FShop.DAL;
using FShop.BAL;

public partial class EditNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string new_ID = Request.QueryString["News_ID"];
            if (ExtendFunctional.IsNumberic(new_ID) == false)
            {
                SetControlForInsert();
            }
            else
                LoadControlForEdit(new_ID);
        }
    }

    private void LoadControlForEdit(string news_ID)
    {
        News news = News_BAL.Get_News_ByID(int.Parse(news_ID));
        if (news != null)
        {
            //Gan control voi cac truong trong doi tuong   
            DropDownList1.SelectedValue = news.Cate_ID +"";         
            txtSource.Value = news.News_Source;
            txtPro_Name.Value = news.News_Title;            
            Image1.ImageUrl = "../Images/Product/" + news.News_Image;
            ASPxHtmlEditor1.Html = news.News_Intro;
            ASPxHtmlEditor2.Html = news.News_Content;            
        }
        btnAdd.Visible = false;
        btnEdit.Visible = true;
    }

    private void SetControlForInsert()
    {
        btnAdd.Visible = true;
        btnEdit.Visible = false;
        //btnBack.Attributes.Add("onclick", "javascript :history.back(1)");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (! string.IsNullOrEmpty(FileUpload1.FileName) )
            {
                if (! ExtendFunctional.IsImageFile(FileUpload1.FileName))
                {
                    MessageBox.Show("Bạn chỉ được phép nhập các file ảnh.");

                    return;
                }
            }
            Insert();
            
        }
        catch (Exception ex)
        {
            MessageBox.Show("Thêm mới không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }

    private void Insert()
    {
        News news = new News();
        news.News_CreateDate = DateTime.Now;
        news.News_Hit = 1;
        news.News_Intro = ASPxHtmlEditor1.Html;
        news.News_Content = ASPxHtmlEditor2.Html;
        news.News_Source = txtSource.Value;
        news.News_Status = true;
        news.News_Title = txtPro_Name.Value;
        news.Cate_ID = int.Parse(DropDownList1.SelectedValue.ToString());
        if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
        {
            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Product/";
            news.News_Image = ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
            Image1.ImageUrl = "../Images/Product/" + news.News_Image;
        }
        News_BAL.Insert_News(news);
        MessageBox.Show("Đã thêm mới thành công."); 
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(FileUpload1.FileName))
            {
                if (!ExtendFunctional.IsImageFile(FileUpload1.FileName))
                {
                    MessageBox.Show("Bạn chỉ được phép nhập các file ảnh.");
                    return;
                }
            }
            Update();

        }
        catch (Exception ex)
        {
            MessageBox.Show("Cập nhật không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }

    private void Update()
    {
        News news = News_BAL.Get_News_ByID(int.Parse (Request.QueryString["News_ID"]));
        news.News_Intro = ASPxHtmlEditor1.Html;
        news.News_Content = ASPxHtmlEditor2.Html;
        news.News_Source = txtSource.Value;
        news.News_Title = txtPro_Name.Value;
        news.Cate_ID = int.Parse(DropDownList1.SelectedValue.ToString());
        if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
        {
            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Product/";
            news.News_Image = ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
            Image1.ImageUrl = "../Images/Product/" + news.News_Image;
        }
        News_BAL.Update_News(news);
        MessageBox.Show("Đã sửa thành công."); 
    }
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageNews.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ASPxHtmlEditor1.Html = "";
        ASPxHtmlEditor2.Html ="";
        txtSource.Value = "";
        txtPro_Name.Value = "";
    }
}
